﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

public partial class _1_6 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //LabelTiShi.Visible = false;

            using (DataClassesDataContext mydata = new DataClassesDataContext())
            {
                var rd = from a in mydata.a01106
                         where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                         where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                         select a;
                foreach (var r in rd)
                {
                    HiddenField1.Value = "edit";//有记录，则置编辑状态
                    TextBoxYSZL.Text = r.USE_COUNT == null ? "" : string.Format("{0:#.####}", r.USE_COUNT);
                    TextBoxXXYS.Text = r.SUM_YEAR == null ? "" : string.Format("{0:#.####}", r.SUM_YEAR);
                    TextBoxZLS.Text = r.TAPWATER_COUNT == null ? "" : string.Format("{0:#.####}", r.TAPWATER_COUNT);
                    TextBoxDXS.Text = r.GROUNDWATER_YEAR == null ? "" : string.Format("{0:#.####}", r.GROUNDWATER_YEAR);
                    TextBoxDBS.Text = r.SURFACEWATER_YEAR == null ? "" : string.Format("{0:#.####}", r.SURFACEWATER_YEAR);
                    TextBoxQTS.Text = r.OTHERWATER_YEAR == null ? "" : string.Format("{0:#.####}", r.OTHERWATER_YEAR);
                    TextBoxCFYSL.Text = r.CIRCLE_COUNT == null ? "" : string.Format("{0:#.####}", r.CIRCLE_COUNT);
                    TextBoxCFYSLV.Text = (r.CIRCLE_PERCENT == null || r.CIRCLE_PERCENT == 0) ? "" : string.Format("{0:#.####}", r.CIRCLE_PERCENT);
                }

            }


        }
    }

    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101106");
    }

    //提交记录
    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) //当所有校验控件都正确是才提交
        {
            //应加入数量级错误的校验


            LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
            LabelTiShi.Visible = true;

            ZhuanHuan zh = new ZhuanHuan();//调用转换类

            using (DataClassesDataContext mydata = new DataClassesDataContext())
            {
                if (HiddenField1.Value == "edit")
                {
                    var rd2 = (from a in mydata.a01106
                               where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                               where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                               select a).First();
                    rd2.USE_COUNT = zh.StrToDouble(TextBoxZLS.Text.Trim()) + zh.StrToDouble(TextBoxDXS.Text.Trim()) + zh.StrToDouble(TextBoxDBS.Text.Trim()) + zh.StrToDouble(TextBoxQTS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim());
                    rd2.SUM_YEAR = zh.StrToDouble(TextBoxZLS.Text.Trim()) + zh.StrToDouble(TextBoxDXS.Text.Trim()) + zh.StrToDouble(TextBoxDBS.Text.Trim()) + zh.StrToDouble(TextBoxQTS.Text.Trim());
                    rd2.TAPWATER_COUNT = zh.StrToDouble(TextBoxZLS.Text.Trim());
                    rd2.GROUNDWATER_YEAR = zh.StrToDouble(TextBoxDXS.Text.Trim());
                    rd2.SURFACEWATER_YEAR = zh.StrToDouble(TextBoxDBS.Text.Trim());
                    rd2.OTHERWATER_YEAR = zh.StrToDouble(TextBoxQTS.Text.Trim());
                    rd2.CIRCLE_COUNT = zh.StrToDouble(TextBoxCFYSL.Text.Trim());
                    if ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0) && (zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0))
                    {
                        rd2.CIRCLE_PERCENT = zh.StrToDouble(TextBoxCFYSL.Text.Trim()) / ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()))) * 100;
                    }
                    else
                    {
                        rd2.CIRCLE_PERCENT = 0;
                    }
                }
                else//新增
                {
                    a01106 p = new a01106();
                    p.qiyeid = Convert.ToInt32(Session["qiyeid"]);
                    p.DATA_MONTH = Convert.ToString(Session["ShenBaoNianDu"]);
                    p.USE_COUNT = zh.StrToDouble(TextBoxZLS.Text.Trim()) + zh.StrToDouble(TextBoxDXS.Text.Trim()) + zh.StrToDouble(TextBoxDBS.Text.Trim()) + zh.StrToDouble(TextBoxQTS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim());
                    p.SUM_YEAR = zh.StrToDouble(TextBoxZLS.Text.Trim()) + zh.StrToDouble(TextBoxDXS.Text.Trim()) + zh.StrToDouble(TextBoxDBS.Text.Trim()) + zh.StrToDouble(TextBoxQTS.Text.Trim());
                    p.TAPWATER_COUNT = zh.StrToDouble(TextBoxZLS.Text.Trim());
                    p.GROUNDWATER_YEAR = zh.StrToDouble(TextBoxDXS.Text.Trim());
                    p.SURFACEWATER_YEAR = zh.StrToDouble(TextBoxDBS.Text.Trim());
                    p.OTHERWATER_YEAR = zh.StrToDouble(TextBoxQTS.Text.Trim());
                    p.CIRCLE_COUNT = zh.StrToDouble(TextBoxCFYSL.Text.Trim());
                    if ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0) && (zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0))
                    {
                        p.CIRCLE_PERCENT = zh.StrToDouble(TextBoxCFYSL.Text.Trim()) / ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()))) * 100;
                    }
                    mydata.a01106.InsertOnSubmit(p);
                }

                mydata.SubmitChanges();
                HiddenField1.Value = "edit";

            }


            //解决提交页面刷新后变动回旧值得问题
            TextBoxXXYS.Text = string.Format("{0:#.####}", zh.StrToDouble(TextBoxZLS.Text.Trim()) + zh.StrToDouble(TextBoxDXS.Text.Trim()) + zh.StrToDouble(TextBoxDBS.Text.Trim()) + zh.StrToDouble(TextBoxQTS.Text.Trim()));
            TextBoxYSZL.Text = string.Format("{0:#.####}", zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()));
            if ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0) && (zh.StrToDouble(TextBoxCFYSL.Text.Trim()) != 0))
            {
                TextBoxCFYSLV.Text = (zh.StrToDouble(TextBoxCFYSL.Text.Trim()) / ((zh.StrToDouble(TextBoxXXYS.Text.Trim()) + zh.StrToDouble(TextBoxCFYSL.Text.Trim()))) * 100).ToString("0.0000");
            }
            else
            {
                TextBoxCFYSLV.Text = "0";
            }

            //LabelTiShi.Visible = false;
            MessageBox("tishi", "你所填写申报内容正确，已成功提交!");
        }
    }


    //// <summary> 
    /// 弹出提示信息框 
    /// </summary> 
    /// <param name="strKey">函数key值</param> 
    /// <param name="strInfo">提示信息</param> 
    public void MessageBox(string strKey, string strInfo)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(strKey))
        {
            string strjs = "alert('" + strInfo + "');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
        }
    }
}